<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Discount extends ORM
{

    //获取可折扣的产品列表
    public static function get_product()
    {
//        $excludeid=array(4,6,7,10,11,12,14,101,106,107,109,111);
        $includeid = array(1,2,3,5,13,114,118,121);//商城:21
        $sql="select * from sline_model where id in (".implode(',',$includeid).") and is_showable=1 and is_orderable=1 ";
        $result=DB::query(Database::SELECT,$sql)->execute()->as_array();
        return $result;
    }

    public static function is_installed()
    {
        return St_Functions::is_normal_app_install('memberdiscount');
    }

    public static function get_order_discount($order_id)
    {
        $amount = 0 ;
        $record = DB::select()->from('member_order_discount')
            ->where('order_id','=',$order_id)->execute()->current();

        if(!empty($record))
        {
            $amount = doubleval($record['amount']);
        }
        $amount = floatval(number_format($amount,2,'.',''));
        return $amount;
    }

    public static function dowload_picture($url)
    {
        return self::download($url);

           include_once(PUBLICPATH.'/vendor/httpdown.class.php');

            if(strpos($url,'//')==0)
            {
                $url = 'http:'.$url;
            }

            if(strpos($url,'http://')===false && strpos($url,'https://')===false && 0)
            {
                return $url;
            }
            if(!stristr($url,$GLOBALS['cfg_basehost']))
            {

                $htd = new HttpDown();//实例化
                $htd->OpenUrl($url);

                $sparr = Array("image peg", "image/jpeg", "image/gif", "image/png", "image/xpng", "image/wbmp");
                if(!in_array($htd->GetHead("content-type"),$sparr))
                {
                    return '';
                }
                else
                {

                    $date=date("Ymd");
                    $name=date("YmdHis").rand(1,50);
                    $path=UPLOADPATH."/main/allimg/";

                    $url=$path.$date."/";
                    $imgUrl = $url.$name;

                    //创建路径
                    if(!file_exists($url))
                    {
                        mkdir($url);
                    }
                    $itype = $htd->GetHead("content-type");

                    if($itype=="image/gif")
                    {
                        $itype = '.gif';
                    }
                    else if($itype=="image/png")
                    {
                        $itype = '.png';
                    }
                    else if($itype=="image/wbmp")
                    {
                        $itype = '.bmp';
                    }
                    else
                    {
                        $itype = '.jpg';
                    }
                    $fileurl = $imgUrl.$itype;
                    $ok = $htd->SaveToBin($fileurl);
                    $litpic = $fileurl;

                    $litpic=str_replace(BASEPATH,'',$litpic);//去掉头
                    $litpic=str_replace('\\','/',$litpic);
                }
            }
            else
            {
                $litpic=$url;
            }
            return $litpic;
    }


    public  static function download($url)
    {
        if(strpos($url,'//')===0)
        {
            $url = 'http:'.$url;
        }
        if(strpos($url,'http://')===false && strpos($url,'https://')===false)
        {
            return $url;
        }

        if(!stristr($url,$GLOBALS['cfg_basehost'])) {

            $date = date("Ymd");
            $name = date("YmdHis") . rand(1, 50);
            $name = preg_replace('/(\/)|(\/)|(\?)|(\#)|(\%)|(\&)|(\=)|(\.)|(\:)/i', '_',$url);
            $path = UPLOADPATH . "/main/allimg/fenxiao";
            if(!file_exists($path))
            {
                mkdir($path);
            }


            $file_base_url = $path .'/';

            if(!file_exists($file_base_url))
            {
                mkdir($file_base_url);
            }
            $imgUrl = $file_base_url . $name;

            $path_info = pathinfo($url);

            $itype = empty($path_info['extension']) ? 'jpg' : $path_info['extension'];


            $fileurl = $imgUrl . '.' . $itype;

            if(file_exists($fileurl))
            {
                $litpic=str_replace(BASEPATH,'',$fileurl);//去掉头
                $litpic=str_replace('\\','/',$litpic);
                return $litpic;
            }


            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
            $file = curl_exec($ch);
            $resource = fopen($fileurl, 'a');
            fwrite($resource, $file);
            fclose($resource);
            $errno = curl_errno($ch);
            curl_close($ch);

            $litpic=str_replace(BASEPATH,'',$fileurl);//去掉头
            $litpic=str_replace('\\','/',$litpic);
            if(empty($errno))
            {
                return $litpic;
            }
            else
            {
                return null;
            }
        }
    }

    public static function download_code($code_url)
    {
        $url =St_Client::get_http_protocol(). $GLOBALS['current_host'].$GLOBALS['cfg_public_url']."vendor/qrcode/make.php?param={$code_url}";
        $date = date("Ymd");
        $name = preg_replace('/(\/)|(\/)|(\?)|(\#)|(\%)|(\&)|(\=)|(\.)|(\:)/i', '_',$code_url);
      // $name = date("YmdHis") . rand(1, 50);
        $path = UPLOADPATH . "/main/allimg/";

        $file_base_url = $path . $date . "/";

        if(!file_exists($file_base_url))
        {
            mkdir($file_base_url);
        }
        $imgUrl = $file_base_url . $name;
        $path_info = pathinfo($url);
        $itype = 'png';
        $fileurl = $imgUrl . '.' . $itype;

        if(file_exists($fileurl))
        {
            $litpic=str_replace(BASEPATH,'',$fileurl);//去掉头
            $litpic=str_replace('\\','/',$litpic);
            return $litpic;
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
        $file = curl_exec($ch);
        $resource = fopen($fileurl, 'a');
        fwrite($resource, $file);
        fclose($resource);
        $errno = curl_errno($ch);
        curl_close($ch);

        $litpic=str_replace(BASEPATH,'',$fileurl);//去掉头
        $litpic=str_replace('\\','/',$litpic);
        if(empty($errno))
        {
            return $litpic;
        }
        else
        {
            return null;
        }
    }

}